import {
  view,
  like,
  option
} from '../../api/blog'
import {
  add,
  list
} from '../../api/blogComment'
// import hljs from 'highlight.js'
// import 'highlight.js/styles/java.css';


let num

// pages/detail/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    blogId: '',
    blog: {
      content: ''
    },
    showCommentBox: false,
    showOtherCommentBox: false,
    commentList: [],
    commentForm: {
      content: ''
    },
    commentLstForm: {
      pageNum: 1
    },
    placeholder: '说说你的想法',
    reachButtom: false,
    sk: true,
    commentClear: '',
    addCommentBtnLoadding: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      blogId: options.id,
      commentForm: {
        blogId: options.id,
        content: ''
      },
      commentListForm: {
        pageNum: 1,
        pageSize: 10,
        blogId: options.id
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    view(this.data.blogId).then(res => {
      this.setData({
        blog: res,
        sk: false
      })
      if (!res.isCollected && !wx.getStorageSync('collect' + res.id)) {
        // 一分钟后还在这个页面就弹出收藏
        num = setTimeout(() => {
          clearTimeout(num)
          wx.showModal({
            content: '觉得不错就收藏起来吧',
            confirmText: '现在收藏',
            cancelText: '下次一定',
            success(res2) {
              if (res2.confirm) {
                const params = {
                  blogId: res.id,
                  type: 2,
                  option: true
                }
                option(params).then(() => {
                  this.data.blog.isCollected = true
                  this.setData({
                    blog: this.data.blog
                  })
                })
              }
              if (res2.cancel) {
                wx.setStorageSync('collect' + res.id, 1)
              }
            }
          })
        }, 60 * 1000)
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    if (num) {
      clearTimeout(num)
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    console.log('yy')
  },

  openCommentBox(event) {
    const e = event
    if (e.currentTarget.dataset.comment) {
      this.setData({
        placeholder: `回复@${e.currentTarget.dataset.comment.nickname}`,
        commentForm: {
          content: e.detail.value,
          blogId: this.data.blogId,
          replyId: e.currentTarget.dataset.comment.id
        },
        showCommentBox: true,
        showOtherCommentBox: false
      })
    } else {
      this.setData({
        placeholder: `说说你的看法`,
        showCommentBox: true,
        commentForm: {
          content: e.detail.value,
          blogId: this.data.blogId,
          replyId: ''
        },
      })
    }
  },
  openOtherCommentBox() {
    this.data.commentListForm.pageNum = 1

    this.setData({
      showOtherCommentBox: true,
      commentListForm: this.data.commentListForm,
      commentList: []
    })
    list(this.data.commentListForm).then(res => {
      this.setData({
        commentList: res.records
      })
    })
  },
  handleButtom() {
    if (this.data.reachButtom) {
      return;
    }
    // wx.showLoading({
    //   title: '加载中',
    //   mask: true
    // })
    this.data.commentListForm.pageNum = this.data.commentListForm.pageNum + 1

    this.setData({
      commentListForm: this.data.commentListForm
    })
    list(this.data.commentListForm).then(res => {
      this.setData({
        commentList: this.data.commentList.concat(res.records)
      })
      if (res.total === this.data.commentList.length) {
        this.setData({
          reachButtom: true
        })
      }
    })
  },
  like() {
    if (this.data.blog.isLiked) {
      wx.showModal({
        content: '你个吊毛点赞过了',
        showCancel: false,
        confirmText: '我知道了'
      })
      return false
    }
    like(this.data.blogId).then(() => {
      this.data.blog.likeCount = this.data.blog.likeCount + 1
      this.data.blog.isLiked = true
      this.setData({
        blog: this.data.blog
      })
    })
  },
  textAreaInput(e) {
    this.setData({
      commentForm: {
        content: e.detail.value,
        blogId: this.data.blogId,
        replyId: this.data.commentForm.replyId
      }
    })
  },
  addComment() {
    if (!this.data.commentForm.content) {
      wx.showToast({
        title: '至少写几个字吧？',
        icon: 'error'
      })
      return false
    }
    this.setData({
      addCommentBtnLoadding: true
    })
    add(this.data.commentForm).then(() => {
      this.data.blog.commentCount = this.data.blog.commentCount + 1
      this.setData({
        blog: this.data.blog,
        showCommentBox: false,
        commentForm: {},
        commentClear: '',
        addCommentBtnLoadding: false
      })

    }).catch(() => {
      this.setData({
        showCommentBox: false,
        commentForm: {},
        commentClear: '',
        addCommentBtnLoadding: false
      })
    })
  },
  onPageScroll: function (e) {
    const scrollTop = e.scrollTop // 当前滚动位置
    if (scrollTop > 100) { // 如果滚动超过 200rpx
      wx.setNavigationBarTitle({
        title: this.data.blog.title
      })
    } else {
      wx.setNavigationBarTitle({
        title: '小程序从入门到放弃'
      })
    }
  }
})